package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessFarmSystem;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.AddressVO;
import com.digitalchina.lxnsy.toolkit.RegexpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.EditTextWithClear;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/8/3 20:17
 * 修改备注：
 * ================================================
 */
public class EditAddressActivity extends BaseActivity {
    @Bind(R.id.edit_address_et_name)
    EditTextWithClear editAddressEtName;
    @Bind(R.id.edit_address_et_phone)
    EditTextWithClear editAddressEtPhone;
    @Bind(R.id.edit_address_tv_area)
    TextView editAddressTvArea;
    @Bind(R.id.edit_address_tv_area_hint)
    TextView editAddressTvAreaHint;
    @Bind(R.id.edit_address_et_detail_address)
    EditTextWithClear editAddressEtDetailAddress;
    @Bind(R.id.edit_address_btn_save)
    Button editAddressBtnSave;

    private AddressVO addressVO;
    private String addressId = "";

    String provinceName;
    String cityName;
    String countyName;
    String detailsName;

    String provinceCode = "", cityCode = "", countyCode = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_edit_address);
    }

    @Override
    public void initMember() {
        if (!getIntent().hasExtra("editAddress"))
            setTitle("新增地址");
        else {
            setTitle("修改地址");

            addressVO = (AddressVO) getIntent().getSerializableExtra("editAddress");
            addressId = addressVO.getId();

            if (null == addressVO.getProvince()) {
                provinceName = "";
            } else {
                provinceCode = addressVO.getProvince().getCode();
                provinceName = addressVO.getProvince().getProvinceName();
            }
            if (null == addressVO.getCity()) {
                cityName = "";
            } else {
                cityCode = addressVO.getCity().getCode();
                cityName = addressVO.getCity().getCityName();
            }

            if (null == addressVO.getCounty())
                countyName = "";
            else {
                countyCode = addressVO.getCounty().getCountyCode();
                countyName = addressVO.getCounty().getCountyName();
            }

            if (null == addressVO.getAddress())
                detailsName = "";
            else
                detailsName = addressVO.getAddress();

            editAddressEtName.setText(addressVO.getUserName());
            editAddressEtName.setSelection(addressVO.getUserName().length());
            editAddressEtPhone.setText(addressVO.getMobilePhone());
            if (provinceCode.equals("110000") || provinceCode.equals("120000") || provinceCode.equals("310000") || provinceCode.equals("500000") || (provinceCode.equals("460000") && cityCode.equals("469000")))
                editAddressTvArea.setText(String.format("%s %s", provinceName, countyName));
            else
                editAddressTvArea.setText(String.format("%s %s %s", provinceName, cityName, countyName));
            editAddressEtDetailAddress.setText(detailsName);
        }

        if (StringUtil.isStrEmpty(editAddressTvArea.getText().toString())) {
            editAddressTvAreaHint.setVisibility(View.VISIBLE);
        } else {
            editAddressTvAreaHint.setVisibility(View.GONE);
        }

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_ADDRESS_EDIT_SUCCESS:
                        if (!getIntent().hasExtra("editAddress"))
                            showToast("新增地址成功");
                        else
                            showToast("修改地址成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_ADDRESS_EDIT_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.edit_address_tv_area, R.id.edit_address_btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_address_tv_area:
                final BottomDialog bottomDialog = new BottomDialog(this);
                bottomDialog.setOnAddressSelectedListener(new OnAddressSelectedListener() {
                    @Override
                    public void onAddressSelected(Province province, City city, County county, Street street) {
                        provinceCode = province.province_code;
                        cityCode = city != null ? city.city_code : "";
                        countyCode = county != null ? county.county_code : "";
                        editAddressTvAreaHint.setVisibility(View.GONE);
                        if (provinceCode.equals("110000") || provinceCode.equals("120000") || provinceCode.equals("310000") || provinceCode.equals("500000") || (provinceCode.equals("460000") && cityCode.equals("469000")))
                            editAddressTvArea.setText(String.format("%s %s", province.province_name, county != null ? county.county_name : ""));
                        else
                            editAddressTvArea.setText(String.format("%s %s %s", province.province_name,
                                    city != null ? city.city_name : "", county != null ? county.county_name : ""));
                        bottomDialog.dismiss();
                    }
                });
                bottomDialog.show();
                break;
            case R.id.edit_address_btn_save:
                if (StringUtil.isEditEmpty(editAddressEtName)) {
                    showSnackbar("收货人不能为空");
                    return;
                }
                if (StringUtil.isEditEmpty(editAddressEtPhone)) {
                    showSnackbar("手机号码不能为空");
                    return;
                }

                if (!RegexpUtil.isRegexpValidate(editAddressEtPhone.getText().toString(), RegexpUtil.MOBILE_REGEXP)) {
                    showSnackbar("请输入合法手机号码");
                    return;
                }

                if (StringUtil.isStrEmpty(editAddressTvArea.getText().toString().trim())) {
                    showSnackbar("所在地区不能为空");
                    return;
                }
                if (StringUtil.isEditEmpty(editAddressEtDetailAddress)) {
                    showSnackbar("详细地址不能为空");
                    return;
                }

                BusinessFarmSystem.editAddress(this, addressId, StringUtil.getEditText(editAddressEtName), provinceCode, cityCode, countyCode, StringUtil.getEditText(editAddressEtDetailAddress), StringUtil.getEditText(editAddressEtPhone), mHandler);
                break;
        }
    }
}
